<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where" @keydown.enter.prevent="reload">
          <a-row>
            <a-form-item label="区域名称:">
              <a-input v-model:value.trim="where.name" placeholder="请输入区域名称" allow-clear />
            </a-form-item>
            <a-form-item label="是否共享:">
              <a-select v-model:value="where.sharing" placeholder="请选择" style="width: 205px">
                <a-select-option value="1">共享区域</a-select-option>
                <a-select-option value="2">非共享区域</a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="id"
          :datasource="url"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >
          <!-- table上边工具栏 -->
          <!--          <template #toolbar>
                      <a-space>
                        <a-button type="primary" @click="openEdit()">
                          <template #icon>
                            <plus-outlined />
                          </template>
                          <span>新建</span>
                        </a-button>
                        <a-button type="danger" @click="removeBatch()">
                          <template #icon>
                            <delete-outlined />
                          </template>
                          <span>删除</span>
                        </a-button>
                      </a-space>
                    </template>-->

          <!-- table列表状态栏 -->
          <!-- 1是激活，2是禁用 -->
          <template #sharing="{ record }">
            <a-switch :checked="record.sharing === 1" @change="checked => editSharing(checked, record)" />
          </template>

          <!-- table操作栏按钮 -->
          <template #action="{ record }">
            <a-space>
              <a @click="openEdit(record)">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定要删除此区域吗？" @confirm="remove(record)">
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
  </div>

  <!-- 编辑弹窗 -->
  <position-edit v-model:visible="showEdit" :data="current" @done="reload" />
</template>

<script>
import { createVNode } from 'vue';
// 删除，感叹号，加号图标
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import PositionEdit from './area-edit.vue';
import { AreaApi } from '@/api/AreaApi';
import { DashboardApi } from '@/api/DashboardApi';

export default {
  name: 'area',
  components: {
    PositionEdit
  },
  data() {
    return {
      // 用户信息
      user: {},
      // 表格数据接口
      url: '/business/api/area/findPage',
      // 表格列配置
      columns: [
        {
          title: '序号',
          key: 'index',
          width: 48,
          customRender: ({ index }) => this.$refs.table.tableIndex + index
        },
        {
          title: '区域名称',
          dataIndex: 'name',
          width: 160,
          sorter: true
        },
        {
          title: '是否共享',
          dataIndex: 'sharing',
          sorter: true,
          align: 'center',
          width: 160,
          slots: { customRender: 'sharing' }
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          width: 160,
          sorter: true
        }

        /*{
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center',
          slots: { customRender: 'action' }
        }*/
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false
    };
  },
  mounted() {
    this.getUser();
  },
  methods: {
    // 获取用户信息
    async getUser() {
      const res = await DashboardApi.getUser();
      this.user = res.data;
    },
    /**
     * 搜索按钮
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     */
    reset() {
      this.where = {};
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除单个
     */
    async remove(row) {
      const result = await AreaApi.del({ id: row.id });
      this.$message.success(result.message);
      this.reload();
    },

    /**
     * 批量删除
     */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm({
        title: '提示',
        content: '确定要删除选中的区域吗?',
        icon: createVNode(ExclamationCircleOutlined),
        maskClosable: true,
        onOk: async () => {
          let params = this.selection.map(d => d.id);
          const result = await AreaApi.batchDel(params);
          this.$message.success(result.message);
          this.reload();
        }
      });
    },

    /**
     * 打开编辑弹窗
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    /**
     * 修改地区共享状态
     */
    async editSharing(checked, row) {
      if(this.user.roleId === '2'){
        return;
      }

      const id = row.id;
      // 共享状态：1-共享，2-不共享
      const sharing = checked ? 1 : 2;
      const result = await AreaApi.edit({ id, sharing });
      this.$message.success(result.message);
      row.sharing = sharing;
    }
  }
};
</script>

<style scoped>
/*搜索框与表格之间的间隙*/
.block-interval {
  margin-bottom: 10px;
}

/*重写顶部搜索表单的样式*/
.ant-form-item {
  margin-bottom: 0;
}
/* 表格斑马样式 **/
/deep/.ant-table-tbody tr:nth-child(2n)
{
  background-color:#fafafa;
}
</style>
